package com.granada.service.base;

import com.granada.behaviour.base.PermissionBehavior;
import com.granada.behaviour.base.RolePermissionBehavior;
import com.granada.behaviour.interfaces.RoleBehavior;
import com.granada.common.CurdWithReturnAdd;
import com.granada.entity.Ao;
import com.granada.entity.SysRole;
import com.granada.model.common.Role;
import com.granada.vo.resp.PageVO;

import java.util.List;

public abstract class RoleService<T> implements CurdWithReturnAdd, RoleBehavior {
    protected Role roleModel;
    protected com.granada.behaviour.base.RoleBehavior roleBehavior;
    protected PermissionBehavior permissionBehavior;
    protected RolePermissionBehavior rolePermissionBehavior;
    protected Ao ao;
    public RoleService(
            Role roleModel,
            com.granada.behaviour.base.RoleBehavior roleBehavior,
            PermissionBehavior permissionBehavior,
            RolePermissionBehavior rolePermissionBehavior,
            Ao ao
        ) {
        this.roleModel = roleModel;
        this.roleBehavior = roleBehavior;
        this.permissionBehavior = permissionBehavior;
        this.rolePermissionBehavior = rolePermissionBehavior;
        this.ao = ao;
    }

    @Override
    public Long add() throws Exception {
        ao.id = this.roleModel.add();
        if(null != ao.getPermissionIds() && !ao.getPermissionIds().isEmpty()){
            rolePermissionBehavior.addBatch(ao.getPermissionIds(),ao.id);
        }
        return ao.id;
    }

    @Override
    public void update() throws Exception {
        this.roleModel.update();
        rolePermissionBehavior.removeByRoleId(ao.id);
        if(null != ao.getPermissionIds() && !ao.getPermissionIds().isEmpty()){
            rolePermissionBehavior.addBatch(ao.getPermissionIds(),ao.id);
            updateAfter(ao.id);
        }
    }

    @Override
    public void delete() throws Exception {
        this.roleModel.delete();
        rolePermissionBehavior.removeByRoleId(ao.id);
        deleteAfter(ao.id);

    }
    @Override
    public void batchDelete() throws Exception {
        roleModel.batchDelete();
        for(Long id: ao.ids){
            deleteAfter(id);
        }
    }

    public void updateAfter(Long roleId) throws Exception {
        roleBehavior.updateAfter(roleId);
    }

    public void deleteAfter(Long roleId) throws Exception {
        roleBehavior.deleteAfter(roleId);
    }

    @Override
    public abstract T getOne() throws Exception;

    @Override
    public T getSearchOne() throws Exception {
        return (T) this.roleModel.getSearchOne();
    }

    @Override
    public List<T> getAll() throws Exception {
        return this.roleModel.getAll();
    }

    @Override
    public List<T> getSearchAll() throws Exception {
        return this.roleModel.getSearchAll();
    }

    @Override
    public PageVO<T> getList() throws Exception {
        return this.roleModel.getList();
    }

    @Override
    public PageVO<T> getSearchList() throws Exception {
        return this.roleModel.getSearchList();
    }

    @Override
    public List<String> getRoleNames(Long userId) throws Exception {
        return this.roleBehavior.getRoleNames(userId);
    }

    @Override
    public List<SysRole> getRoleInfoByUserId(Long userId) throws Exception {
        return  this.roleBehavior.getRoleInfoByUserId(userId);
    }
}
